package com.repair.dao.Operator.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.repair.dao.Operator.IOperatorDao;

import com.repair.domain.Module;
import com.repair.domain.Operator;
import com.repair.factory.HibernateSessionFactory;

public class OperatorDao implements IOperatorDao {

	@Override
	public void saveOperator(Operator obj) {
		Session session = HibernateSessionFactory.getSession();

		try {
			Transaction tx = session.beginTransaction();
			if (session != null) {
				session.saveOrUpdate(obj);
				tx.commit();
				session.flush();
				session.clear();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.flush();
				session.clear();	
			}
		}
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Operator> getAllOperator() {
		List<Operator> list = new ArrayList<Operator>();
		Session session = HibernateSessionFactory.getSession();
		try {
			if (session != null) {
				String sql = "select * from OPERATOR";
				SQLQuery query = session.createSQLQuery(sql);
				list = query.addEntity(Operator.class).list();
			}
		} catch (Exception e) {

		} finally {
			if (session != null) {
				session.flush();
				session.clear();
			}
		}
		return list;

	}

	@Override
	public void deleteOperator(String id) {
		Session session = HibernateSessionFactory.getSession();
		try {
			Transaction tx = session.beginTransaction();
			if (session != null) {
				String sql = "delete from OPERATOR where OPERATOR_ID in ("+id+")";
				SQLQuery query = session.createSQLQuery(sql);
				query.executeUpdate();
				tx.commit();
			}
		} catch (Exception e) {

		} finally {
			if (session != null) {
				session.flush();
				session.clear();
			}
		}

	}

	public Operator isOperator(String username, String password) {
		Operator o=null;
		List<Operator> operator = new ArrayList<Operator>();
		Session session = HibernateSessionFactory.getSession();
		try {
			if (session != null) {
				String sql = "select * from OPERATOR where OPERATOR_ID='"+username+"' and OPERATOR_PWD='"+password+"'";
				SQLQuery query = session.createSQLQuery(sql);
				operator = query.addEntity(Operator.class).list();
				for (Operator item : operator) {
					o=item;
				}
			}
		} catch (Exception e) {

		} finally {
			if (session != null) {
				session.flush();
				session.clear();
			}
		}
		return o;
		
	}
	
	public List<Operator> serchoperator(String key) {
		Operator o=null;
		List<Operator> operator = new ArrayList<Operator>();
		Session session = HibernateSessionFactory.getSession();
		try {
			if (session != null) {
				String sql = "select * from OPERATOR where OPERATOR_ID like '%"+key+"%' or OPERATOR_NAME like '%"+key+"%' or OPERATOR_PWD like '%"+key+"%' or RULE_ID like '%"+key+"%'";
				SQLQuery query = session.createSQLQuery(sql);
				operator = query.addEntity(Operator.class).list();
				
			}
		} catch (Exception e) {

		} finally {
			if (session != null) {
				session.flush();
				session.clear();
			}
		}
		return operator;
		
	}
	
	
}
